﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace GmailNotifier
{
    public partial class MailList : UserControl
    {
        private int _mailCount;

        public MailList()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        public void Add(string mailSubject, string from, string link)
        {
            // place to field with out link
            var toLabel = new Label
                              {
                                  AutoEllipsis = true,
                                  BackColor = Color.Transparent,
                                  Size = new Size(Width - 25, 13),
                                  Location = new Point(3, (_mailCount*50) + 3),
                                  Text = "From : " + from
                              };
            // place subject field in a link lebel, put the link into the tag field
            var mailLabel = new LinkLabel
                                {
                                    AutoEllipsis = true,
                                    BackColor = Color.Transparent,
                                    Size = new Size(Width - 25, 37),
                                    Location = new Point(3, (_mailCount*50) + 16),
                                    Text = mailSubject,
                                    Tag = link,
                                    LinkBehavior = LinkBehavior.HoverUnderline
                                };
            // increase the mail count so that the border will place after each mail
            _mailCount++;

            var border = new KryptonBorderEdge
                             {
                                 Location = new Point(3, (_mailCount*50) + 3),
                                 Size = new Size(Width - 25, 5)
                             };
            mailLabel.LinkClicked += MailLabelClick;
            mailPanel.Controls.Add(toLabel);
            mailPanel.Controls.Add(mailLabel);
            mailPanel.Controls.Add(border);
            mailPanel.Refresh();
            mailPanel.Invalidate();
        }

        private static void MailLabelClick(object sender, EventArgs e)
        {
            Process.Start(((Label) sender).Tag.ToString());
        }

        internal void Clear()
        {
            mailPanel.Controls.Clear();
            mailPanel.Refresh();
            mailPanel.Invalidate();
            _mailCount = 0;
        }
    }
}